<template>
	<view ref="mescrollRef" class="mescroll">
		<slot></slot>
	</view>
</template>

<script>
    export default {
        name: "PullRefresh",
        props: {
            httpOption: Object,
        },
        data() {
            return {
                mescroll: null
            }
        },
        methods: {
            initMescroll() {
                const {loadData, refreshSucc, refreshFail, loadMoreSucc, loadMoreFail} = this.httpOption
                const self = this
                const Mescroll = require('/src/libs/mescroll-1.4.2')
                this.mescroll = new Mescroll(this.$refs.mescrollRef.$el, {
                    up: {
                        auto: false,//是否在初始化完毕之后自动执行一次下拉刷新的回调
                        isBounce: false,//是否允许ios的bounce回弹;默认true,允许回弹 (v 1.3.0新增)
                        isBoth: true,//下拉刷新时,如果滑动到列表底部是否可以同时触发上拉加载
                        callback(page) {
                            //console.log(undefined.aa)
                            const {num} = page
                            loadData(num, {
                                onSucc(code, msg, info) {
                                    if (num === 1) {
                                        if (refreshSucc) {
                                            refreshSucc(code, msg, info)
                                        }
                                        self.$nextTick(() => {
                                            if (self.mescroll) {
                                                self.mescroll.endSuccess()
                                            }
                                        })
                                    } else {
                                        let dataSize = 0
                                        if (loadMoreSucc) {
                                            dataSize = loadMoreSucc(code, msg, info)
                                        }
                                        if (dataSize > 0) {
                                            self.$nextTick(() => {
                                                if (self.mescroll) {
                                                    self.mescroll.endSuccess()
                                                }
                                            })
                                        } else {
                                            self.$nextTick(() => {
                                                if (self.mescroll) {
                                                    self.mescroll.endSuccess(0, false)
                                                }
                                            })
                                        }
                                    }
                                },
                                onFail() {
                                    if (num === 1) {
                                        if (refreshFail) {
                                            refreshFail()
                                        }
                                    } else {
                                        if (loadMoreFail) {
                                            loadMoreFail()
                                        }
                                    }
                                    self.$nextTick(() => {
                                        if (self.mescroll) {
                                            self.mescroll.endErr()
                                        }
                                    })
                                }
                            })
                        },
                    }
                })
            },
            /**
             * 加载第一页数据
             */
            refreshData(){
                this.$nextTick(() => {
                    if (this.mescroll){
                        this.mescroll.resetUpScroll()
                    }
                })
            },
        },
        mounted() {
            this.$nextTick(this.initMescroll)
        },
        destroyed() {
            if (this.mescroll) {
                this.mescroll.destroy()
            }
        }
    }
</script>
